package net.lab1024.smartadmin.module.weapp.users.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.lab1024.smartadmin.common.domain.PageResultDTO;
import net.lab1024.smartadmin.common.domain.ResponseDTO;
import net.lab1024.smartadmin.module.system.employee.domain.bo.EmployeeBO;
import net.lab1024.smartadmin.module.system.employee.domain.entity.EmployeeEntity;
import net.lab1024.smartadmin.module.weapp.users.dao.UsersDao;
import net.lab1024.smartadmin.module.weapp.users.domain.bo.WxUsersBO;
import net.lab1024.smartadmin.module.weapp.users.domain.dto.UsersAddDTO;
import net.lab1024.smartadmin.module.weapp.users.domain.dto.UsersUpdateDTO;
import net.lab1024.smartadmin.module.weapp.users.domain.dto.UsersQueryDTO;
import net.lab1024.smartadmin.module.weapp.users.domain.entity.UsersEntity;
import net.lab1024.smartadmin.module.weapp.users.domain.vo.UsersVO;
import net.lab1024.smartadmin.module.weapp.users.domain.vo.UsersExcelVO;
import net.lab1024.smartadmin.util.SmartPageUtil;
import net.lab1024.smartadmin.util.SmartBeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

/**
 * [ 微信小程序 用户信息表 ]
 *
 * @author wuqian
 * @version 1.0
 * @company 五千
 * @copyright (c)  五千Inc. All rights reserved.
 * @date 2021-03-09 14:13:28
 * @since JDK1.8
 */
@Service
public class UsersService {

    @Autowired
    private UsersDao usersDao;

    /**
     * 员工基本信息的缓存
     */
    private static final ConcurrentHashMap<String, WxUsersBO> wxUserCache = new ConcurrentHashMap<>();

    public UsersEntity getById(Long id){
        return  usersDao.selectById(id);
    }

    /**
     * 根据openid查询
     */
    public WxUsersBO getByOpenid(String openid){
        WxUsersBO wxUsersBO = wxUserCache.get(openid);
        if (wxUsersBO == null) {
            UsersVO usersVO = usersDao.selectByOpenid(openid);
            UsersEntity usersEntity = SmartBeanUtil.copy(usersVO,UsersEntity.class);
            if (usersEntity != null) {
                wxUsersBO = new WxUsersBO(usersEntity);
                wxUserCache.put(usersEntity.getOpenid(), wxUsersBO);
            }
        }
        return wxUsersBO;
    }

    /**
     * 分页查询
     * @author wuqian
     * @date 2021-03-09 14:13:28
     */
    public ResponseDTO<PageResultDTO<UsersVO>> queryByPage(UsersQueryDTO queryDTO) {
        Page page = SmartPageUtil.convert2QueryPage(queryDTO);
        IPage<UsersVO> voList = usersDao.queryByPage(page, queryDTO);
        PageResultDTO<UsersVO> pageResultDTO = SmartPageUtil.convert2PageResult(voList);
        return ResponseDTO.succData(pageResultDTO);
    }

    /**
     * 添加
     * @author wuqian
     * @date 2021-03-09 14:13:28
     */
    public ResponseDTO<String> add(UsersAddDTO addDTO) {
        UsersEntity entity = SmartBeanUtil.copy(addDTO, UsersEntity.class);
        usersDao.insert(entity);
        return ResponseDTO.succ();
    }

    /**
     * 编辑
     * @author wuqian
     * @date 2021-03-09 14:13:28
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> update(UsersUpdateDTO updateDTO) {
        UsersEntity entity = SmartBeanUtil.copy(updateDTO, UsersEntity.class);
        usersDao.updateById(entity);
        return ResponseDTO.succ();
    }

    /**
     * 删除
     * @author wuqian
     * @date 2021-03-09 14:13:28
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> deleteByIds(List<Long> idList) {
        usersDao.deleteByIdList(idList);
        return ResponseDTO.succ();
    }

    /**
     * 查询全部导出对象
     * @author wuqian
     * @date 2021-03-09 14:13:28
     */
    public List<UsersExcelVO> queryAllExportData(UsersQueryDTO queryDTO) {
        return usersDao.queryAllExportData( queryDTO);
    }

    /**
     * 批量查询导出对象
     * @author wuqian
     * @date 2021-03-09 14:13:28
     */
    public List<UsersExcelVO> queryBatchExportData(List<Long> idList) {
        return usersDao.queryBatchExportData(idList);
    }
}
